package com.lokiy.kit.ext

import android.annotation.SuppressLint
import android.util.Size
import android.view.MotionEvent
import android.view.View

/**
 *
 * @author Lokiy
 * @date 2023-08-04 18:02
 */

data class AreaClickItem(val parent: Size, val clickArea: Size, val start: Int, val top: Int)

@SuppressLint("ClickableViewAccessibility")
fun View.setOnAreaClick(item: AreaClickItem, onClick: (Boolean) -> Unit) {
    var isInArea = false

    setOnTouchListener { _, event ->
        if (event.action == MotionEvent.ACTION_DOWN) {
            val scale = item.parent.width * 1f / width
            isInArea = (event.x * scale - item.start).toInt() in 0..(item.clickArea.width) &&
                    (event.y * scale - item.top).toInt() in 0..(item.clickArea.height)
        }
        false
    }
    setOnClickListener {
        onClick.invoke(isInArea)
    }
}